class AuthorsController < ApplicationController
  before_filter :load_session
  after_filter :save_session

  private
    def load_session
      @countries = session[:author_countries] || Country.find(:all, :order=>:name)
      @filter_first_name = session[:filter_first_name] ||''
      @filter_last_name = session[:filter_last_name] || ''
      @filter_country = session[:filter_country] || -1
    end

    def save_session
      session[:author_countries] = @countries
      session[:filter_first_name] = @filter_first_name
      session[:filter_last_name] = @filter_last_name
      session[:filter_country] = @filter_country
    end

    def execute_query
      items_per_page = 2
      conditions = ["upper(first_name) like upper(?) and upper(last_name) like upper(?) and (country_id = ? or ?=-1)", "%" + @filter_first_name + "%", "%" + @filter_last_name + "%", @filter_country, @filter_country]
      order = "first_name, last_name"

      @total = Author.count(:conditions => conditions)
      @authors = Author.paginate :per_page => items_per_page,:page => params[:page],
                  :conditions => conditions,
                  :order => order
    end

  public
    # GET /authors
    # GET /authors.xml
    def index
      execute_query

      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @authors }
      end
    end

    def list
      puts "entre al list"
      @filter_first_name = params['search_first_name'] || ''
      @filter_last_name = params['search_last_name'] || ''
      @filter_country = params['search_country'] || -1
      if @filter_country == ''
        @filter_country = -1
      end
      execute_query
      render :partial=>'authors_list' , :layout=>false
    end

    # GET /authors/1
    # GET /authors/1.xml
    def show
      @author = Author.find(params[:id])

      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @author }
      end
    end

    # GET /authors/new
    # GET /authors/new.xml
    def new
      @author = Author.new

      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @author }
      end
    end

    # GET /authors/1/edit
    def edit
      @author = Author.find(params[:id])
    end

    # POST /authors
    # POST /authors.xml
    def create
      @author = Author.new(params[:author])

      respond_to do |format|
        if @author.save
          flash[:notice] = t('msg_record_successfully_created')
          format.html { redirect_to(@author) }
          format.xml  { render :xml => @author, :status => :created, :location => @author }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @author.errors, :status => :unprocessable_entity }
        end
      end
    end

    # PUT /authors/1
    # PUT /authors/1.xml
    def update
      @author = Author.find(params[:id])

      respond_to do |format|
        if @author.update_attributes(params[:author])
          flash[:notice] = t('msg_record_successfully_updated')
          format.html { redirect_to(@author) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @author.errors, :status => :unprocessable_entity }
        end
      end
    end

    # DELETE /authors/1
    # DELETE /authors/1.xml
    def destroy
      @author = Author.find(params[:id])
      @author.destroy

      respond_to do |format|
        format.html { redirect_to(authors_url) }
        format.xml  { head :ok }
      end
    end
end

